package _151_Reverse_Words_in_a_String;

import java.util.LinkedList;

/**
 * Created by 孙加辉 on 2018/7/22.
 */
class Solution {
    public String reverseWords(String s) {
        LinkedList<String> list = new LinkedList<String>();
        String temp = "";
        for(int i=0;i<s.length();i++){
            if(s.charAt(i)==' '){
                if(!temp.equals("")){
                    list.add(temp);
                    temp = "";
                }
            }
            else
                temp+=""+s.charAt(i);
        }
        if(!temp.equals("")){
            list.add(temp);
        }
        String res = "";
        for(int i=list.size()-1;i>0;i--){
            res+=list.get(i)+" ";
        }
        if(list.size()>0)
            res+=list.get(0);
        return res;
    }
}
public class _151_Reverse_Words_in_a_String {
    public static void main(String[] args){
        Solution solution = new Solution();
        System.out.println(solution.reverseWords("123 456 789"));
    }
}
